<?php

App::import('module','admin/admincontroller');

Class BannerController extends AdminController
{    
	public $uses = array('banner');
	
	public $comp = array('upload');
	
	public function index()
    {      	
    	$filtro = new QueryFilter();
    	$filtro->command = 'ORDER BY id DESC';
    	
		$Banners = $this->Banner->all($filtro);
		$this->set('banners',$Banners);	
    }

    public function add()
    {
    	if(!empty($this->data))
    	{
    		if(!empty($this->files['img1']) && ($this->files['img1']->isImage || $this->files['img1']->isSWF))
    		{
    			$upload = new Upload();
    			$nome = $upload->GerarNome($this->files['img1']->extensao,false);	
    			$upload->setFolder('files/banner/');							
				if($upload->salvar($this->files['img1']->tmp_name,$nome))
				{
					$this->data['Banner']['file'] = $nome;
					$id = $this->Banner->Save($this->data);
					$this->setShow('Banner Salvo com sucesso');   
    				$this->_context->redirectUrl('admin.banner');
				}
    		}
    		$this->setShow('banner tem que ser Imagem ou Swf');    		
    	}
    }
  
     public function view($id ='')
    {    	
    	if(empty($id))
    	{
    		$this->_context->redirectUrl('admin.banner');
    	}   	
    	$banner = $this->Banner->findForId($id);    		
    	$this->set(compact('banner'));  	
    }    
  
    public function del($id)
    {    	
    	if(!$id)
    	{    		
    		$this->_context->redirectUrl('admin.banner');
    	}

    	$banner = $this->Banner->findForId($id); 
    	if(file_exists('files/banner/'.$banner['file']))
    	{
    		@unlink('files/banner/'.$banner['file']);
    	}
    	$this->Banner->Delete($id); 
    	$this->_context->redirectUrl('admin.banner');
    }
    
    public function Setup()
    {
    	parent::Setup();  
    	$this->set('tipo',array(1=>'image',2=>'swf')); 	 	
    }
}
?>
